package com.kyg.tjtoolmanager.utils;

import com.kyg.tjtoolmanager.App;
import com.kyg.tjtoolmanager.db.SQLiteUtils;
import javafx.scene.image.Image;

import java.io.ByteArrayInputStream;
import java.net.*;
import java.util.Base64;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Utils {

    static String regex = "^((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)$";

    public static String getLocalIP() {
        String osName = System.getProperty("os.name").toUpperCase();
        if (osName.startsWith("WINDOWS")) {
            return getWindowLocalIp();
        } else if (osName.startsWith("LINUX")) {
            return getLinuxLocalIp();
        }
        return "127.0.0.1";
    }

    private static String getWindowLocalIp() {
        String ipAddress = "";
        try {
            InetAddress localHost = InetAddress.getLocalHost();
            ipAddress = localHost.getHostAddress();
        } catch (UnknownHostException e) {
            throw new RuntimeException(e);
        }
        return ipAddress;
    }

    private static String getLinuxLocalIp(){
        String ip = "";
        try {
            for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
                NetworkInterface intf = en.nextElement();
                String name = intf.getName();
                if (!name.contains("docker") && !name.contains("lo")) {
                    for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                        InetAddress inetAddress = enumIpAddr.nextElement();
                        if (!inetAddress.isLoopbackAddress()) {
                            String ipaddress = inetAddress.getHostAddress().toString();
                            if (!ipaddress.contains("::") && !ipaddress.contains("0:0:") && !ipaddress.contains("fe80")) {
                                ip = ipaddress;
                            }
                        }
                    }
                }
            }
        } catch (SocketException ex) {
            ex.printStackTrace();
        }
        return ip;
    }

    public static String getIPAddress() {
        try {
            List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
            for (NetworkInterface intf : interfaces) {
                List<InetAddress> addrs = Collections.list(intf.getInetAddresses());
                for (InetAddress addr : addrs) {
                    if (!addr.isLoopbackAddress() && addr instanceof Inet4Address) {
                        return addr.getHostAddress();
                    }
                }
            }
        } catch (Exception e) {
            SQLiteUtils.insertLog("getIPAddress error:" + e.toString());
        }
        return "";
    }

    public static boolean validateIp(String ip) {
        Pattern regexPattern = Pattern.compile(regex);
        Matcher matcher = regexPattern.matcher(ip);
        return matcher.matches();
    }

    public static Image base64StringToImage(String base64) throws IllegalArgumentException {
        String data = base64.substring(base64.indexOf(",") + 1);
//        SQLiteUtils.insertLog("图片：" + data);
        byte[] imageBytes = Base64.getDecoder().decode(data);
        return new Image(new ByteArrayInputStream(imageBytes));
    }

}
